﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.IBLL;
using System.Data;
using HZ.Supervise.Model;
using System.Text;

namespace HZ.Supervise.Web.Secondphase.Print
{
    public partial class _Default : BasePage
    {
        public _Default()
        {
            this.ModuleNo = Module.医院统计分析.ToString();
            this.PageOperateType = OperateType.催缴通知书.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindDropDownList(); 
                this.AreaControl1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                lblDayNow.Text = DateTime.Now.Day.ToString();                
                lblMonthNow.Text = DateTime.Now.Month.ToString();                
                lblYearNow.Text = DateTime.Now.Year.ToString();
            }
          
        }

        //IBLL_User ic = HZ.Supervise.BLLFactory.BLL_User();

        protected void BindDropDownList()
        {
            //Role role=new Role();
            //role.RoleID="05";
            //int TotalCount = 0;
            //IList<HZ.Supervise.Model.User> lstOrgName = ic.GetAllUserByRole(role, 1, 0, out TotalCount);
            //ddlOrgName.DataSource = lstOrgName;
            //ddlOrgName.DataTextField = "UserName";
            //ddlOrgName.DataValueField = "UserID";
            //ddlOrgName.DataBind();
        }

        protected void btnPreview_Click(object sender, EventArgs e)
        {
            string areaid = AreaControl1.SelectedAreaID;
            StringBuilder str = new StringBuilder();
            
            if (String.IsNullOrEmpty(txtTimeFrom.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('请选择开始时间！');</script>");
            }
            if (String.IsNullOrEmpty(txtTimeTo.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('请选择结束时间！');</script>");
            }
            DateTime? timeFrom = new HZ.Common.ConvertHelper(txtTimeFrom.Text).ToDateTime;
            DateTime? timeTo = new HZ.Common.ConvertHelper(txtTimeTo.Text).ToDateTime;
            if (areaid.EndsWith("00") || areaid.EndsWith("0000"))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('地区请选择到县区！');</script>");
            }
            if (timeFrom >= timeTo)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('结束时间小于或等于开始时间，请重新设置时间！');</script>");
            }
            string strwhere = "";

            switch (ddlHosType.SelectedValue)
            {
                case "0":
                    break;
                case "1":
                    strwhere=" and Levels in('3','4','5','6')";
                    break;
                case "2":
                    strwhere=" and IsCountryDetail in('1','2','3','4')";
                    break;
                case "3":
                    strwhere=" and IsCountryDetail ='2'";
                    break;
                case "4":
                    strwhere=" and IsCountryDetail ='1'";
                    break;
                default:
                    break;
            }

            if (!string.IsNullOrEmpty(ddlIsGov.SelectedValue))
            {
                strwhere += " and IsGovernment="+ddlIsGov.SelectedValue;               
            }

            string strAreaName = "";
            IBLL_Area ia = HZ.Supervise.BLLFactory.IBLL_Area();

            if (areaid.EndsWith("79"))
            {
                strAreaName = HZ.Supervise.Web.Function.GetAreaNameBySTR(areaid);
                areaid = HZ.Supervise.Web.Function.GetAreaIDBySTR(areaid);
                
            }
            else
            {
                Area selectArea = ia.GetArea(areaid);
               string fatherArea = ia.GetAreaNamesByID(selectArea.FatherAreaID);
               strAreaName = fatherArea + selectArea.AreaName;
            }
           
            IBLL_Statistics ibs = HZ.Supervise.BLLFactory.IBLL_Statistics();
            
            DataTable dt = ibs.GetHospitalAmountStatistics(areaid, timeFrom, timeTo,strwhere);
            DataTable dtdetail = ibs.GetHospitalAmountStatisticsGroupByArea(areaid, timeFrom, timeTo,strwhere);
            DataTable dtTotal = ibs.GetHospitalAmountStatisticsGroupByAreaTotal(areaid, timeFrom, timeTo, strwhere);
            DataTable dtTmp = new DataTable();
            dtTmp = dtdetail.Clone();
            int count = 1;
            if (dt.Rows.Count > 0 && dtdetail.Rows.Count > 0)
            {
                string tabhead = @"<tr>
                        <td style='width: 1em;'>
                            序号
                        </td>
                        
                        <td style='width: 5em;'>
                            基层医疗卫生机构名称
                        </td>
                        <td style='width: 4em;'>
                            所属月份
                        </td>
                        <td style='width: 3em;'>
                            入库单金额
                        </td>
                        <td style='width: 3em;'>
                            退货单金额
                        </td>
                        <td style='width: 3em;' >
                            应缴款金额
                        </td>
                    </tr>";
                if (AreaControl1.SelectedAreaID.EndsWith("79"))
                {
                    tabhead = @"<tr>
                        <td style='width: 1em;'>
                            序号
                        </td>
                        <td style='width: 1.5em;'>
                            地区
                        </td>
                        <td style='width: 5em;'>
                            基层医疗卫生机构名称
                        </td>
                        <td style='width: 4em;'>
                            所属月份
                        </td>
                        <td style='width: 3em;'>
                            入库单金额
                        </td>
                        <td style='width: 3em;'>
                            退货单金额
                        </td>
                        <td style='width: 3em;' >
                            应缴款金额
                        </td>
                    </tr>";
                }
                litHead.Text = tabhead;
                AmountConvertToChinese actc = new AmountConvertToChinese();
                lblAmount.Text = "￥" + dt.Rows[0]["payamount"].ToString();
                lblAreaName.Text = strAreaName+"卫生局、财政局";
                lblChineseAmount.Text = actc.ConvertSum(dt.Rows[0]["payamount"].ToString());
                lblDayFrom.Text = Convert.ToDateTime(timeFrom).Day.ToString();
                lblDayNow.Text = DateTime.Now.Day.ToString();
                lblDayTo.Text = Convert.ToDateTime(timeTo).Day.ToString();
                lblHospitalCount.Text = dt.Rows[0]["hospitalcount"].ToString();
                lblMonthFrom.Text = Convert.ToDateTime(timeFrom).Month.ToString();
                lblMonthNow.Text = DateTime.Now.Month.ToString();
                lblMonthTo.Text = Convert.ToDateTime(timeTo).Month.ToString();
                lblOrgName.Text = strAreaName;
                lblYearFrom.Text = Convert.ToDateTime(timeFrom).Year.ToString();
                lblYearNow.Text = DateTime.Now.Year.ToString();
                lblYearTo.Text = Convert.ToDateTime(timeTo).Year.ToString();

                decimal warehouseamount = 0;
                decimal returnamount = 0;
                decimal payamount = 0;
                foreach (DataRow dr in dtTotal.Rows)
                {

                    dtTmp = GetNewDataTable(dtdetail, string.Format("AreaID='{0}'", dr["AreaID"]));
                    for (int i = 0; i < dtTmp.Rows.Count; i++)
                    {

                        str.Append("<tr><td>");
                        str.Append((count++) + "");
                        if (i == 0 && AreaControl1.SelectedAreaID.EndsWith("79"))
                        {
                            str.Append(string.Format("</td><td rowspan='{0}'>", dtTmp.Rows.Count));
                            str.Append(dr["AreaName"].ToString());
                        }
                        str.Append("</td><td>");
                        str.Append(dtTmp.Rows[i]["HospitalName"].ToString());
                        str.Append("</td><td>");
                        str.Append(dtTmp.Rows[i]["WarehouseMonth"].ToString());
                        str.Append("</td><td>");
                        str.Append(dtTmp.Rows[i]["WarehouseAmount"].ToString());
                        str.Append("</td><td>");
                        str.Append(dtTmp.Rows[i]["ReturnAmount"].ToString());
                        str.Append("</td><td>");
                        str.Append(dtTmp.Rows[i]["PayAmount"].ToString());
                        str.Append("</td></tr>");
                    }
                    if (AreaControl1.SelectedAreaID.EndsWith("79"))
                    {
                        str.Append("<tr><td>");
                        str.Append(""); 
                        str.Append("</td><td>");
                        str.Append(""); 
                        str.Append("</td><td>");
                        str.Append("小计：");
                        str.Append("</td><td>");
                        str.Append("");
                        str.Append("</td><td>");
                        str.Append(dr["WarehouseAmount"].ToString());
                        str.Append("</td><td>");
                        str.Append(dr["ReturnAmount"].ToString());
                        str.Append("</td><td>");
                        str.Append(dr["PayAmount"].ToString());
                        str.Append("</td></tr>"); 
                    }
                    litContent.Text = str.ToString();
                    warehouseamount += Convert.ToDecimal(dr["WarehouseAmount"].ToString());
                    returnamount += Convert.ToDecimal(dr["ReturnAmount"].ToString());
                    payamount += Convert.ToDecimal(dr["PayAmount"].ToString());
                }
                str.Append("<tr><td>");
                str.Append("");//(dtdetail.Rows.Count+1) + "");
                if (AreaControl1.SelectedAreaID.EndsWith("79"))
                {
                    str.Append("</td><td>");
                    str.Append("");
                }
                str.Append("</td><td>");
                str.Append("合计：");
                str.Append("</td><td>");
                str.Append("");
                str.Append("</td><td>");
                str.Append(warehouseamount.ToString());
                str.Append("</td><td>");
                str.Append(returnamount.ToString());
                str.Append("</td><td>");
                str.Append(payamount.ToString());
                str.Append("</td></tr>");
                litContent.Text = str.ToString();
                btnPrint.Visible = true;
                btnPrintDetail.Visible = true;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('所选地区在所选时间段内无数据，请重新填写条件！');</script>");
                btnPrint.Visible = false;
                btnPrintDetail.Visible = false;
            }
            //if (dt.Rows.Count > 0 && dtdetail.Rows.Count > 0)
            //{
            //    AmountConvertToChinese actc = new AmountConvertToChinese();
            //    lblAmount.Text = "￥" + dt.Rows[0]["payamount"].ToString();
            //    lblAreaName.Text = strAreaName;
            //    lblChineseAmount.Text = actc.ConvertSum(dt.Rows[0]["payamount"].ToString());
            //    lblDayFrom.Text = Convert.ToDateTime(timeFrom).Day.ToString();
            //    lblDayNow.Text = DateTime.Now.Day.ToString();
            //    lblDayTo.Text = Convert.ToDateTime(timeTo).Day.ToString();
            //    lblHospitalCount.Text = dt.Rows[0]["hospitalcount"].ToString();
            //    lblMonthFrom.Text = Convert.ToDateTime(timeFrom).Month.ToString();
            //    lblMonthNow.Text = DateTime.Now.Month.ToString();
            //    lblMonthTo.Text = Convert.ToDateTime(timeTo).Month.ToString();
            //    lblOrgName.Text = strAreaName;
            //    lblYearFrom.Text = Convert.ToDateTime(timeFrom).Year.ToString();
            //    lblYearNow.Text = DateTime.Now.Year.ToString();
            //    lblYearTo.Text = Convert.ToDateTime(timeTo).Year.ToString();

            //    decimal warehouseamount = 0;
            //    decimal returnamount = 0;
            //    decimal payamount = 0;
            //    for (int i = 0; i < dtdetail.Rows.Count; i++)
            //    {
            //        str.Append("<tr><td>");
            //        str.Append((i+1) + "");
            //        str.Append("</td><td>");
            //        str.Append(dtdetail.Rows[i]["AreaName"].ToString());
            //        str.Append("</td><td>");
            //        str.Append(dtdetail.Rows[i]["HospitalName"].ToString());
            //        str.Append("</td><td>");
            //        str.Append(dtdetail.Rows[i]["WarehouseMonth"].ToString());
            //        str.Append("</td><td>");
            //        str.Append(dtdetail.Rows[i]["WarehouseAmount"].ToString());
            //        str.Append("</td><td>");
            //        str.Append(dtdetail.Rows[i]["ReturnAmount"].ToString());
            //        str.Append("</td><td>");
            //        str.Append(dtdetail.Rows[i]["PayAmount"].ToString());
            //        str.Append("</td></tr>");
            //        warehouseamount += Convert.ToDecimal(dtdetail.Rows[i]["WarehouseAmount"].ToString());
            //        returnamount += Convert.ToDecimal(dtdetail.Rows[i]["ReturnAmount"].ToString());
            //        payamount += Convert.ToDecimal(dtdetail.Rows[i]["PayAmount"].ToString());
            //    }
            //    str.Append("<tr><td>");
            //    str.Append("");//(dtdetail.Rows.Count+1) + "");
            //    str.Append("</td><td>");
            //    str.Append("合计：");
            //    str.Append("</td><td>");
            //    str.Append("");
            //    str.Append("</td><td>");
            //    str.Append(warehouseamount.ToString());
            //    str.Append("</td><td>");
            //    str.Append(returnamount.ToString());
            //    str.Append("</td><td>");
            //    str.Append(payamount.ToString());
            //    str.Append("</td></tr>");
            //    litContent.Text = str.ToString();
            //    btnPrint.Visible = true;
            //    btnPrintDetail.Visible = true;
            //}
            //else
            //{
            //    ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('所选地区在所选时间段内无数据，请重新填写条件！');</script>");
            //    btnPrint.Visible = false;
            //    btnPrintDetail.Visible = false;
            //}
        }
        private DataTable GetNewDataTable(DataTable dt, string condition)
        {
            DataTable newdt = new DataTable();
            newdt = dt.Clone();
            DataRow[] dr = dt.Select(condition);
            for (int i = 0; i < dr.Length; i++)
            {
                newdt.ImportRow((DataRow)dr[i]);
            }
            return newdt; 
        }
    }
}
